/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SpyForm.java
 *
 * Created on 2009-7-14, 14:04:22
 */

package spyware;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.GroupLayout;
import javax.swing.JComponent;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 *
 * @author macitu
 */
public class SpyForm extends javax.swing.JFrame {
	
	/** Contains function for communication with the server*/
	static ServiceInvoker service;
    /** Creates new form SpyForm */
    public SpyForm() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menu = new javax.swing.JTabbedPane();
        proportiestab = new javax.swing.JPanel();
        GroupLayout proportiestabLayout = new GroupLayout((JComponent)proportiestab);
        proportiestab.setLayout(proportiestabLayout);
        IPlabel = new javax.swing.JLabel();
        ip_one = new javax.swing.JTextField();
        ip_two = new javax.swing.JTextField();
        ip_three = new javax.swing.JTextField();
        ip_four = new javax.swing.JTextField();
        portLabel = new javax.swing.JLabel();
        port = new javax.swing.JTextField();
        go_button = new javax.swing.JButton();
        menutab = new javax.swing.JPanel();
        welcomelabel = new javax.swing.JLabel();
        ping_button = new javax.swing.JButton();
        scr_shot_button = new javax.swing.JButton();
        processes_button = new javax.swing.JButton();
        exit_button = new javax.swing.JButton();
        infotab = new javax.swing.JPanel();
        infoLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Welcome home :)");
        setBackground(new java.awt.Color(51, 51, 51));
        setFont(new java.awt.Font("Curlz MT", 1, 18)); // NOI18N
        setName("mainframe"); // NOI18N

        menu.setBackground(new java.awt.Color(0, 0, 0));
        menu.setForeground(new java.awt.Color(255, 0, 255));
        menu.setFont(new java.awt.Font("Jokerman", 1, 14)); // NOI18N

        proportiestab.setBackground(new java.awt.Color(0, 0, 0));
        proportiestab.setForeground(new java.awt.Color(0, 255, 0));

        IPlabel.setBackground(new java.awt.Color(0, 0, 0));
        IPlabel.setFont(new java.awt.Font("Jokerman", 1, 14)); // NOI18N
        IPlabel.setForeground(new java.awt.Color(51, 255, 0));
        IPlabel.setText("Enter Port");

        ip_one.setBackground(new java.awt.Color(51, 51, 51));
        ip_one.setFont(new java.awt.Font("Jokerman", 1, 12)); // NOI18N
        ip_one.setForeground(new java.awt.Color(255, 0, 255));
        ip_one.setText("127");

        ip_two.setBackground(new java.awt.Color(51, 51, 51));
        ip_two.setFont(new java.awt.Font("Jokerman", 1, 12)); // NOI18N
        ip_two.setForeground(new java.awt.Color(255, 0, 255));
        ip_two.setText("0");

        ip_three.setBackground(new java.awt.Color(51, 51, 51));
        ip_three.setFont(new java.awt.Font("Jokerman", 1, 12)); // NOI18N
        ip_three.setForeground(new java.awt.Color(255, 0, 255));
        ip_three.setText("0");

        ip_four.setBackground(new java.awt.Color(51, 51, 51));
        ip_four.setFont(new java.awt.Font("Jokerman", 1, 12)); // NOI18N
        ip_four.setForeground(new java.awt.Color(255, 0, 255));
        ip_four.setText("1");

        portLabel.setBackground(new java.awt.Color(0, 0, 0));
        portLabel.setFont(new java.awt.Font("Jokerman", 1, 14)); // NOI18N
        portLabel.setForeground(new java.awt.Color(51, 255, 0));
        portLabel.setText("Enter IP");

        port.setBackground(new java.awt.Color(51, 51, 51));
        port.setFont(new java.awt.Font("Jokerman", 1, 12)); // NOI18N
        port.setForeground(new java.awt.Color(255, 0, 255));
        port.setText("18881");

        go_button.setBackground(new java.awt.Color(51, 51, 51));
        go_button.setFont(new java.awt.Font("Jokerman", 1, 18)); // NOI18N
        go_button.setForeground(new java.awt.Color(204, 0, 204));
        go_button.setText("connect");
        proportiestabLayout.setVerticalGroup(proportiestabLayout.createSequentialGroup()
        	.addContainerGap(21, 21)
        	.addComponent(portLabel, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	.addGroup(proportiestabLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	    .addComponent(ip_one, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(ip_two, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(ip_three, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(ip_four, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
        	.addGap(20)
        	.addComponent(IPlabel, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	.addComponent(port, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	.addGap(18)
        	.addComponent(go_button, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	.addContainerGap(55, Short.MAX_VALUE));
        proportiestabLayout.setHorizontalGroup(proportiestabLayout.createSequentialGroup()
        	.addContainerGap()
        	.addGroup(proportiestabLayout.createParallelGroup()
        	    .addGroup(GroupLayout.Alignment.LEADING, proportiestabLayout.createSequentialGroup()
        	        .addGroup(proportiestabLayout.createParallelGroup()
        	            .addGroup(GroupLayout.Alignment.LEADING, proportiestabLayout.createSequentialGroup()
        	                .addComponent(ip_one, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
        	                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        	                .addComponent(ip_two, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE))
        	            .addGroup(GroupLayout.Alignment.LEADING, proportiestabLayout.createSequentialGroup()
        	                .addComponent(port, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
        	                .addGap(6)))
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED, 1, Short.MAX_VALUE)
        	        .addComponent(ip_three, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
        	        .addGap(12))
        	    .addGroup(proportiestabLayout.createSequentialGroup()
        	        .addComponent(portLabel, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 0, Short.MAX_VALUE))
        	    .addGroup(proportiestabLayout.createSequentialGroup()
        	        .addComponent(IPlabel, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 0, Short.MAX_VALUE))
        	    .addGroup(GroupLayout.Alignment.LEADING, proportiestabLayout.createSequentialGroup()
        	        .addComponent(go_button, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 23, Short.MAX_VALUE)))
        	.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        	.addComponent(ip_four, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
        	.addContainerGap(229, 229));
        go_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                go_buttonActionPerformed(evt);
            }
        });

        menu.addTab("set connection", proportiestab);

        menutab.setBackground(new java.awt.Color(0, 0, 0));
        menutab.setForeground(new java.awt.Color(255, 0, 255));
        menutab.setFont(new java.awt.Font("Jokerman", 1, 14)); // NOI18N

        welcomelabel.setBackground(new java.awt.Color(0, 0, 0));
        welcomelabel.setFont(new java.awt.Font("Jokerman", 1, 14)); // NOI18N
        welcomelabel.setForeground(new java.awt.Color(0, 255, 0));
        welcomelabel.setText("Hallo , Master ! Ready to act 007 ?");

        ping_button.setBackground(new java.awt.Color(51, 51, 51));
        ping_button.setFont(new java.awt.Font("Jokerman", 1, 12)); // NOI18N
        ping_button.setForeground(new java.awt.Color(255, 0, 255));
        ping_button.setText("PING");
        
        ping_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ping_buttonActionPerformed(evt);
            }

        });

        scr_shot_button.setBackground(new java.awt.Color(51, 51, 51));
        scr_shot_button.setFont(new java.awt.Font("Jokerman", 1, 12)); // NOI18N
        scr_shot_button.setForeground(new java.awt.Color(255, 0, 255));
        scr_shot_button.setText("TAKE ScreenShot");
        
        scr_shot_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scr_shot_buttonActionPerformed(evt);
            }
        });

        processes_button.setBackground(new java.awt.Color(51, 51, 51));
        processes_button.setFont(new java.awt.Font("Jokerman", 1, 12)); // NOI18N
        processes_button.setForeground(new java.awt.Color(255, 0, 255));
        processes_button.setText("GET Processes");
        
        processes_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                processes_buttonActionPerformed(evt);
            }
        });

        exit_button.setBackground(new java.awt.Color(51, 51, 51));
        exit_button.setFont(new java.awt.Font("Jokerman", 1, 12)); // NOI18N
        exit_button.setForeground(new java.awt.Color(255, 0, 255));
        exit_button.setText("NOT IN A MOOD");

        exit_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exit_buttonActionPerformed(evt);
            }
        });
        
        javax.swing.GroupLayout menutabLayout = new javax.swing.GroupLayout(menutab);
        menutab.setLayout(menutabLayout);
        menutabLayout.setHorizontalGroup(
            menutabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(menutabLayout.createSequentialGroup()
                .addGroup(menutabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(menutabLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(welcomelabel, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(menutabLayout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(menutabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(scr_shot_button, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(processes_button, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(exit_button, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ping_button, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(84, Short.MAX_VALUE))
        );
        menutabLayout.setVerticalGroup(
            menutabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(menutabLayout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(welcomelabel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23)
                .addComponent(ping_button)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scr_shot_button)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(processes_button)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(exit_button)
                .addContainerGap(61, Short.MAX_VALUE))
        );

        menu.addTab("menu", menutab);

        infotab.setBackground(new java.awt.Color(0, 0, 0));
        infotab.setForeground(new java.awt.Color(0, 255, 0));

        infoLabel.setBackground(new java.awt.Color(0, 0, 0));
        infoLabel.setFont(new java.awt.Font("Jokerman", 0, 12)); // NOI18N
        infoLabel.setForeground(new java.awt.Color(0, 255, 0));
        infoLabel.setText("http://www.noway.go/");

        javax.swing.GroupLayout infotabLayout = new javax.swing.GroupLayout(infotab);
        infotab.setLayout(infotabLayout);
        infotabLayout.setHorizontalGroup(
            infotabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infotabLayout.createSequentialGroup()
                .addGap(119, 119, 119)
                .addComponent(infoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(128, Short.MAX_VALUE))
        );
        infotabLayout.setVerticalGroup(
            infotabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infotabLayout.createSequentialGroup()
                .addGap(111, 111, 111)
                .addComponent(infoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(119, Short.MAX_VALUE))
        );

        menu.addTab("info", infotab);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(menu, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(menu, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        getAccessibleContext().setAccessibleName("mainframe");

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     *  This method takes and checks the IP and the port
     *  If problem occurs warning message pops up
     *  else we are connected to the server;
     */
    private void go_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_go_buttonActionPerformed
        int ip1 = 0,ip2 = 0,ip3 = 0,ip4 = 0,portme = 0;
        try{
            ip1 = Integer.parseInt(ip_one.getText());
            ip2 = Integer.parseInt(ip_two.getText());
            ip3 = Integer.parseInt(ip_three.getText());
            ip4 = Integer.parseInt(ip_four.getText());
            portme = Integer.parseInt(port.getText());
        }catch(NumberFormatException ex){
            JOptionPane.showMessageDialog(this,"WrongIP");
        }
        StringBuffer buff = new StringBuffer();
        boolean ipOK = false ;
        boolean portOk = false;
        boolean connected = false;
        String IP = null;
        if( !(ip1 > 0 && ip1 <= 255 && ip2 >= 0 && ip2 <= 255 && ip3 >= 0 &&
                ip3 <= 255 && ip4 >= 0 && ip4 < 255 )){
             JOptionPane.showMessageDialog(this,"WrongIP");
        }
        else{
            buff.append(ip1);
            buff.append(".");
            buff.append(ip2);
            buff.append(".");
            buff.append(ip3);
            buff.append(".");
            buff.append(ip4);
            IP = buff.toString();
            ipOK = true;
        }
        if( !(portme > 1024 && portme < 65525))
           JOptionPane.showMessageDialog(this,"WrongPort");
        else 
        	portOk = true;
        if(ipOK && portOk)
        {
        	while(!connected){
        		if(!(connected = service.connect(IP,portme)))
        		{
        			JOptionPane.showMessageDialog(this,"No connection can be established with this IP");
        			return;
        		}
        		else 
        			JOptionPane.showMessageDialog(this,"Session started ");
        	}
        }
    }//GEN-LAST:event_go_buttonActionPerformed

    private void ping_buttonActionPerformed(ActionEvent evt) {
	    if(service.ping())
	    	JOptionPane.showMessageDialog(this,"Server has been detected");
	}
    
    
    private void processes_buttonActionPerformed(ActionEvent evt) {
		service.getProcesses();
	}
    
    private void scr_shot_buttonActionPerformed(ActionEvent evt) {    	
		service.getScreenshot();		
	}
    
    private void exit_buttonActionPerformed(ActionEvent evt) {
		if(service.exit())
			JOptionPane.showMessageDialog(this,"Session ended!");;
	}
    
	/**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SpyForm().setVisible(true);
            }
        });
        service = new ServiceInvoker();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel IPlabel;
    private javax.swing.JButton exit_button;
    private javax.swing.JButton go_button;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JPanel infotab;
    private javax.swing.JTextField ip_four;
    private javax.swing.JTextField ip_one;
    private javax.swing.JTextField ip_three;
    private javax.swing.JTextField ip_two;
    private javax.swing.JTabbedPane menu;
    private javax.swing.JPanel menutab;
    private javax.swing.JButton ping_button;
    private javax.swing.JTextField port;
    private javax.swing.JLabel portLabel;
    private javax.swing.JButton processes_button;
    private javax.swing.JPanel proportiestab;
    private javax.swing.JButton scr_shot_button;
    private javax.swing.JLabel welcomelabel;
    // End of variables declaration//GEN-END:variables

}
